class Request{
  constructor(){
		this.isShowLoad = false
    this.isSendIng = false
    this.isMany = false
    this.taskList = []
		this.axios = axios.create({
			baseURL : BASEURL,
			timeout : 60000,
			headers : {
				post : {
					'Content-Type' : 'application/json'
				}
			}
		})
		
		/* this.axios.defaults.baseURL = baseUrl
		this.axios.defaults.timeout = 60000
		this.axios.defaults.headers.post['Content-Type'] = 'application/json' */
		this.config = {}
  }

	setBaseURL(baseUrl){
		this.config.baseURL = baseUrl
		//this.axios.defaults.baseURL = baseUrl
	}

  setMany(flag = true){
    this.isMany = flag
  }

	showLoading(text = ''){
		this.isShowLoad = true
		Vue.$indicator.open({text : text})
	}

	addTask(url, callback, data, method = 'get'){
		//let task = this.taskList.shift()
		this.taskList.push({url : url, data : data, callback : callback, method : method.toLowerCase()})
	}

	task(){
		let task = this.taskList.shift()
		if(!task){
			return
		}
		this[task.method](task.url, res => {
			task.callback(res)
			
		}, task.data)
	}

  post(url,callback,data = {}){
    this.config.method = 'POST'
		this.config.data = data
    this.send(url, callback, data)
		return this
  }

  get(url,callback,params = {}){
    this.config.method = 'GET'
		this.config.params = params
    this.send(url, callback, params)
		return this
  }

	put(url,callback,data = {}){
		this.config.method = 'PUT'
		this.config.data = data
		this.send(url, callback, data)
		return this
	}
	
	delete(url,callback,data = {}){
		this.config.method = 'DELETE'
		this.config.data = data
		this.send(url, callback, data)
		return this
	}
	

  send(url,callback){
    if (this.isSendIng && !this.isMany) return
		this.config.headers = {}
		//this.config.headers.Authorization = 'eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NDQ0MzU2NTUsInN1YiI6IjEiLCJleHAiOjE1NDQ0MzU5NTV9.dS91bxo-TXVMqVY3_lLg_Jo5JUoqRhQVFy9hTI7NN6I'
    this.isSendIng = true
    this.config.url = url

		this.axios.request(this.config).then(response => {
			
			if(this.isShowLoad){
				this.isShowLoad = false

			}
			
			this.isSendIng = false
			let repdata = response.data
			if(repdata && 'message' in repdata){
				repdata.msg = repdata.message
			}
			callback(response.data)
		}).catch(e => {
			if(this.isShowLoad){
				this.isShowLoad = false
				
			}
			this.isSendIng = false
			callback({code : 500, msg : '网络错误'})
		})
  }

  abort(){

  }
}